package org.wmq.housewebadmin.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.wmq.housemodel.entity.LeaseTerm;
import org.wmq.housemodel.entity.Result;
import org.wmq.housewebadmin.mapper.LeaseTermMapper;
import org.wmq.housewebadmin.service.LeaseTermService;

import java.util.List;

@Service
@RequiredArgsConstructor
public class LeaseTermServiceImpl implements LeaseTermService {
    private final LeaseTermMapper leaseTermMapper;

    @Override
    public List<LeaseTerm> getList() {
        return this.leaseTermMapper.selectAll();
    }

    @Override
    public Result<Void> saveOrUpdate(LeaseTerm leaseTerm) {
        if (leaseTerm.getId() == null){
            if (this.leaseTermMapper.insert(leaseTerm) == 0){
                return Result.error("id已存在");
            }else{
                return Result.ok();
            }
        }else{
            if (this.leaseTermMapper.updateById(leaseTerm) == 0){
                return Result.error("id不存在");
            }else{
                return Result.ok();
            }
        }
    }

    @Override
    public boolean deleteById(Long id) {
        LeaseTerm leaseTerm = new LeaseTerm();
        leaseTerm.setId(id);
        return this.leaseTermMapper.deleteById(leaseTerm) == 1;
    }
}
